package com.millstein.educenter.controller;


import com.millstein.commonutils.JwtUtils;
import com.millstein.commonutils.Result;
import com.millstein.educenter.entity.UcenterMember;
import com.millstein.educenter.entity.vo.RegisterVo;
import com.millstein.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-26
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UcenterMember member) {
        String token = ucenterMemberService.login(member);
        return Result.ok().data("token", token);
    }

    /**
     * 注册
     * @param vo
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo vo) {
        ucenterMemberService.register(vo);
        return Result.ok();
    }

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 根据id查询用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return Result.ok().data("userInfo", member);

    }

}

